from typing import List


class Solution:
    def findLongestWord(self, s: str, dictionary: List[str]) -> str:
        dictionary.sort(key=lambda x: (-len(x), x))
        res = ""
        for t in dictionary:
            i = j = 0
            while i < len(t) and j < len(s):
                if t[i] == s[j]:
                    i += 1
                j += 1
            if i == len(t):
                if len(t) > len(res) or (len(t) == len(res) and t < res):
                    res = t
        return res


if __name__ == "__main__":
    print(Solution().findLongestWord(s="abpcplea", dictionary=["ale", "apple", "monkey", "plea"]))  # apple
    print(Solution().findLongestWord(s="abpcplea", dictionary=["a", "b", "c"]))  # a
